<?php get_header(); ?>
<!--end header-->
<div class="span-16" id="main-content">
     <?php
     global $wp_query;
     $postid = $wp_query->post->ID;
     ?>
    <h3 class="label-title">Nhà trọ</h3>
    <?php
    /*
     *     Query Nhà Trọ infomation - display numbers of latest Nhà Trọ
     */

    if(have_posts()) : while(have_posts()) :the_post(); ?>

        <?php

        setPostViews($postid);

        $the_id = $postid;
        //set post view

        /*
        * Get important information
        */
        $diachi = get_post_meta($the_id,'_diachi',true);
        $dientich = get_post_meta($the_id,'_dientich',true);
        $gioitinh = get_post_meta($the_id,'_gioitinh',true);
        $ghichu = get_post_meta($the_id,'_ghichu',false);
        $toida =  get_post_meta($the_id,'_toida',true);
        $phongtrong = get_post_meta($the_id,'_phongtrong',true);

        /*
        * Price Information
        */
        $giaphong = get_post_meta($the_id,'_giaphong',true);
        $giadien = get_post_meta($the_id,'_giadien',true);
        $gianuoc = get_post_meta($the_id,'_gianuoc',false);
        $giainternet = get_post_meta($the_id,'_giainternet',true);
        $chiphikhac = get_post_meta($the_id,'_chiphikhac',false);

        /*
        * noi quy
        */
        $lienhe = get_post_meta($the_id,'_lienhe',true);
        $khaosat =  get_post_meta($the_id,'_khaosat',true);
        $noiquy = get_post_meta($the_id,'_noiquy',true);

        if(strcmp($gioitinh,'nam') == 0) $gioitinh = 'Nam';
        elseif(strcmp($gioitinh,'nu') == 0) $gioitinh = 'Nữ';
        else $gioitinh = "Nam & Nữ";

        if(strcmp($phongtrong,'')==0) $phongtrong = "chưa updated";
        if(strcmp($giaphong,'')==0) $giaphong = "chưa updated";
        if(strcmp($toida,'')==0) $toida = "chưa updated";
        if(strcmp($diachi,'')==0) $diachi = "chưa updated";
        if(strcmp($giadien,'')==0) $giadien = "chưa updated";
        if(strcmp($gianuoc[0][0],'')==0) $gianuoc[0][0] = "chưa updated";
        if(strcmp($chiphikhac[0][0],'')==0) $chiphikhac[0][0] = "chưa updated";
        if(strcmp($dientich,'')==0) $dientich = "chưa updated";
        if(strcmp($noiquy,'')==0) $noiquy = "chưa updated";
        if(strcmp($giainternet,'')==0) $giainternet = "chưa updated";
        ?>

        <div class="span-16 item item-single">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

            <?php include('post-meta.php'); ?>
            <div id="slideshow-container">
                <div id="example">
                    <div id="slides">
                        <div class="slides_container">
                            <?php
                            /*
                            * Get more image
                            */
                            global $wpdb, $post;

                            $meta = get_post_meta( $the_id, '_screenshot', false );
                            if ( !is_array( $meta ) )
                                $meta = ( array ) $meta;

                            if ( !empty( $meta ) ) {
                                $meta = implode( ',', $meta );
                                $images = $wpdb->get_col( "
                                SELECT ID FROM $wpdb->posts
                                WHERE post_type = 'attachment'
                                AND ID IN ( $meta )
                                ORDER BY menu_order ASC
                            " );
                                foreach ( $images as $att ) {
                                    // Get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
                                    $src = wp_get_attachment_image_src( $att, array(570,270) );   //527,240)
                                    $src = $src[0];

                                    // Show image
                                    echo "<a href='{$src}'><img src='{$src}' alt =''/></a>";
                                }
                            }
                            else echo "<p>Hình chụp chưa được tải lên!</p>";

                            ?>
                        </div>
                        <a href="#" class="prev"><img src="<?php bloginfo('template_url');?>/images/arrow-prev.png" width="24" height="43" alt="Arrow Prev"></a>
                        <a href="#" class="next"><img src="<?php bloginfo('template_url');?>/images/arrow-next.png" width="24" height="43" alt="Arrow Next"></a>
                    </div>
                    <img src="<?php bloginfo('template_url');?>/images/example-frame.png" width="739" height="341" alt="Example Frame" id="frame">
                </div>
            </div>

            <div class="span-16">
                <ul class="item-single-info">
                    <li><span class="address-label">Địa chỉ nhà :</span><span class="location"><?php echo $diachi; ?></span></li>
                    <li class="size"><span>Diện tích </span><?php echo $dientich ;?></li>
                    <li class="gender"><span>Nhà (phòng) trọ dành cho </span><?php echo $gioitinh ;?></li>
                    <li class="maximum"><span>Số người tối đa ở trong phòng </span><?php echo $toida ;?></li>

                    <li class="empty-room"><span>Số phòng trống hiện tại </span><?php echo $phongtrong; ?></li>
                    <?php if(!empty($ghichu) && strcmp(trim($ghichu[0][0]),'') != 0) { ?>
                    <li class="item-note"><span>Ghi chú</span>
                        <ul class="notes">
                            <?php foreach($ghichu[0] as $value) {
                            echo "<li>$value</li>";
                        }?>
                        </ul>
                    </li>

                    <?php } ;?>
                </ul><!--end list properties-->
                <div class="google-map">
                     <h3>Bản đồ google map</h3>
                     <?php the_content(); ?>
                </div><!--end google-map-->
                <ul class="price-info item-single-info">
                    <li><span>Tiền thuê phòng </span><span class="rent-price"><?php echo $giaphong; ?></span></li>
                    <li class="electric-price"><span>Tiền điện </span><?php echo $giadien; ?> / kWh</li>

                    <li class="water-price"><span>Giá nước</span>
                        <ul class="water">
                            <?php foreach($gianuoc[0] as $value) {
                            echo "<li>$value</li>";
                        }   ?>
                        </ul>
                    </li>
                    <li class="internet-price"><span>Tiền Internet </span> <?php echo $giainternet; ?></li>
                    <li class="others-price"><span>Chi phí khác</span>
                        <ul class="others">
                            <?php foreach($chiphikhac[0] as $value) {
                            echo "<li>$value</li>";
                        }   ?>
                        </ul>
                    </li>
                </ul><!--end price-info-->
                <div class="rules">
                    <h3>Nội quy:</h3>
                    <?php echo trim($noiquy," "); ?>

                </div><!--end noiquy-->

                <ul class="last-information item-single-info">
                    <li class="lienhe"><span>SĐT Liên Hệ Chủ Nhà </span><span class="sdt"><?php echo $lienhe; ?></span> </li>
                </ul><!--end last-infomation-->
                <div class="rules">
                    <h3>Nhận xét người khảo sát:</h3>
                    <?php echo trim($khaosat," "); ?>
                </div><!--end noiquy-->

            </div><!--end item-single-info-->
        </div><!--end item-->
        <?php endwhile; else: echo "Chưa có nhà trọ được đăng!"; endif; ?>

    <?php wp_reset_query(); ?>

     <!-- TrungDQ: include bottom pagination-->
     <?php include ('bot-nav.php'); ?>

     <!-- TrungDQ: comments here -->
     <div class="span-16" id="comment">
         <?php comments_template('', true); ?>
     </div>
     <!-- end comments -->

    <!--related single-post-->
    <div class="related span-16">
        <h3>Nhà trọ liên quan</h3>
        <div id="related">
            <?php
            $cats = get_the_category($post->ID);
            $catID = array();
            $catName = array();
            ?>
            <?php
            foreach($cats as $cat) {
                $catID[] = $cat->cat_ID;
                $catName[] = $cat->name;
            }?>

            <?php
            //echo"<br>ID: ";
            //print_r($catID);
            $name = implode($catName,',');
            //echo"<br>Name: ";
            //print_r($name);

            //set the argument for the wp-loop
            $args = array(
                'posts_per_page' => '3',
                'orderby' =>'rand',
                'category__in' =>$catID,
                'post__not_in'=>array($post->ID)
            );
            //call the constructor
            $relate = new WP_Query($args);
            ?>
            <ul>
                <?php
                //loop
                if($relate->have_posts()) : while($relate->have_posts()) : $relate->the_post(); ?>
                    <li>
                        <div class="related-item">
                            <?php
                            $postid = get_the_id();
                            $phongtrong = get_post_meta($postid,'_phongtrong',true);
                            $giaphong = get_post_meta($postid,'_giaphong',true);
                            $gioitinh = get_post_meta($postid,'_gioitinh',true);
                            if(strcmp($gioitinh,'nam') == 0) $gioitinh = 'Nam';
                            elseif(strcmp($gioitinh,'nu') == 0) $gioitinh = 'Nữ';
                            else $gioitinh = "Nam & Nữ";

                            if(strcmp($phongtrong,'')==0) $phongtrong = "chưa updated";
                            if(strcmp($giaphong,'')==0) $giaphong = "chưa updated";
                            /*
                            * Display thumbnail
                            */
                            if ( has_post_thumbnail() )
                                the_post_thumbnail(array(100,100));
                            else echo 'Hình chưa được tải lên';
                            ?>
                            <div class="related-item-info">
                                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                                <ul>
                                    <li class="location"><span>Địa điểm: </span><?php echo $diachi; ?></li>
                                    <li class="price"><span>Giá: </span><?php echo $giaphong; ?></li>
                                    <li class="empty-room"><span>Số phòng trống hiện tại: </span><?php echo $phongtrong; ?></li>
                                    <?php if(!empty($ghichu) && strcmp(trim($ghichu[0][0]),'') != 0) { ?>
                                    <li class="gender"><span>Phòng dành cho: </span><?php echo $gioitinh; ?></li>
                                    <?php } ;?>
                                </ul><!--end list properties-->
                            </div>

                        </div><!--end random-item-->
                    </li>
                    <?php endwhile; else : echo"Không tìm thấy nhà trọ liên quan"; endif ?>
                <?php wp_reset_postdata(); ?>
            </ul>
        </div>
    </div><!--end related-single-post-->


</div><!--end main-content-->
<!--begin sidebar-->
<?php get_sidebar(); ?>
<!--end sidebar-->
<!--begin footer-->
<?php get_footer(); ?>
<!--end footer-->